#include "font.h"


bool font_init(void)
{
    return (FcInit() == FcTrue);
}

void font_deinit(void)
{
    FcFini();
}

void font_print(font_pattern_t *pattern)
{
    FcPatternPrint(pattern);
}

// <families>-<point sizes>:<name1>=<values1>:<name2>=<values2>...
font_pattern_t * font_query(const char *font)
{
    font_pattern_t *fquery, *fmatch;
    FcResult result;

    fquery = FcNameParse((FcChar8 *)font);
    FcDefaultSubstitute(fquery);
    if(FcConfigSubstitute(NULL, fquery, FcMatchPattern) == FcFalse) {
        printf("font config substitution failed!\n");
    }
#if 0
    font_print(fquery);
    printf("=========================================================\n");
#endif
    fmatch = FcFontMatch(NULL, fquery, &result);
    FcPatternDestroy(fquery);
    if(result == FcResultMatch) {
#if 0
        FcValue fc_file;
        FcPatternGet(fmatch, FC_FILE, 0, &fc_file);
        printf("found the font in the file: %s\n", (const char *)fc_file.u.s);
#endif
        return fmatch;
    }
    if(result == FcResultNoMatch) {
        printf("font match failed!\n");
    }
    else {
        printf("font match error!\n");
    }
    return NULL;
}


double font_get_pixelsize(font_pattern_t *pattern)
{
    FcValue pixelsize;
    if(FcPatternGet(pattern, FC_PIXEL_SIZE,0, &pixelsize) == FcResultMatch) {
        return pixelsize.u.d;
    }
    printf("font can't get pixelsize.\n");
    return 0;
}
